<?php
namespace app\api\service;
use think\Controller;
use think\DB;

class MemberMoney extends Controller {
    static protected $err_code = 0;
    // 用户资金转移接口
    static public function member_money_shift($param) {
        if(!isset($param['member_id']) || !isset($param['money'])
            || !isset($param['current_roles_type']) || !isset($param['source_roles_type'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        // 启动事务
        Db::startTrans();
        try{
            // 根据用户类型  获取对应角色的余额
            $models = ['member', 'repair', 'dealer', 'partner'];
            $model = $models[$param['source_roles_type']];
            if ($param['source_roles_type'] == 0) {
                $where['id'] = $param['member_id'];
            } else {
                $where['member_id'] = $param['member_id'];
            }

            $money = Db::table('mob_' . $model)->where($where)->value('money');
            if( $money < $param['money'] ){
                self::$err_code = 400040;
                throw new \Exception('FAULT');
            }

            $data = [
                [
                    'member_id' => $param['member_id']
                    ,'money' => $param['money']
                    ,'frozen_status' => 0
                    ,'type' => $param['source_roles_type']
                    ,'msg' => "用户资金转移"
                    ,'source' => 0
                    ,'add_time' => time()
                    ,'pay_type' => '资金转出' // 交易方式
                ],
                [
                    'member_id' => $param['member_id']
                    ,'money' => $param['money']
                    ,'frozen_status' => 0
                    ,'type' => $param['current_roles_type']
                    ,'msg' => "用户资金转移"
                    ,'source' => $param['source_roles_type']
                    ,'add_time' => time()
                    ,'pay_type' => '资金转入' // 交易方式
                ]
            ];
            Db::table('mob_member_money')->insertAll($data);
            Db::commit();
            return ['data'=>'','code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }


    // 获取用户账单接口
    static public function member_money_bill($param){
        if(!isset($param['member_id']) || !isset($param['type'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        try{
            $where = [
                'member_id' => $param['member_id'],
                'type' => $param['type'],
                'source' => ['in', '6, 8, 9, 13']
            ];
            if(isset($param['start_time']) && $param['start_time'] > 0  && isset($param['end_time']) && $param['end_time'] > 0) {
                $where['request_time'] = ['GT', $param['start_time']];
                $where['request_time'] = ['LT', $param['end_time']];
            }
            $member_money = db('member_money')->where($where)->order('id desc')->paginate();

            return ['data'=>$member_money->toArray(),'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }


    // 获取用户待结算金额明细接口
    static public function member_money_bill_f($param){
        if(!isset($param['member_id']) || !isset($param['type'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        try{
            $repair = db('repair')->field('id,money, money_frozen')->where('member_id', $param['member_id'])->find();
            $order = db('order_server')->alias('o')->field('mm.*,oa.return_money,oa.add_time as oa_add_time,ops.product_name')
                ->join('mob_member_money mm', 'o.order_sn = mm.order_sn and mm.type=1','LEFT')
                ->join('mob_order_austin oa', 'o.id = oa.order_id and oa.audit_type=1 and (oa.status=1 or oa.result=2) ','LEFT')
                ->join('mob_order_product_server ops', 'oa.product_id = ops.id ','LEFT')
                ->where('o.repair_id', $repair['id'])
                ->where('o.status', 3)->order('o.id desc')->paginate();

            return ['data'=>$order->toArray(),'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }
    
    // 获取用户账单明细详情接口
    static public function get_detail_info($param) {
        if(!isset($param['detail_id']) || !isset($param['member_id'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        try {
            $data = db('member_money')->where('id', $param['detail_id'])->where('member_id', $param['member_id'])->find();
            return ['code' => 200, 'data' => $data, 'msg' => 'SUCCESS'];
        }catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 获取用户钱包首页接口
    static public function get_member_wallet($param) {
        if(!isset($param['member_id']) || !isset($param['type'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        try{
            // 获取余额
            $money = Db::table('mob_member_money')->where('member_id', $param['member_id'])->where('type', $param['type'])->where('frozen_status', 0)->sum('money');
            $money = $money ? $money : 0;

            // 获取银行卡数量
            $bank_cards = Db::table('mob_member_fund_account')->where('member_id', $param['member_id'])->where('type', 3)->count();

            // 获取待结算金额
            switch ($param['type']){
                case 1: // 维修店
                    $repair_id = Db::table('mob_repair')->where('member_id', $param['member_id'])->value('id');
                    $order_price = Db::table('mob_order_server')->where('repair_id', $repair_id)->where('status', 'in', '3, 4')->sum('order_price');
                    break;
                case 2: // 经销商
                    $shop_id = Db::table('mob_shop')->where('member_id', $param['member_id'])->value('id'); // 经销商实际对应的是店铺
                    $order_price = Db::table('mob_order_buy')->where('shop_id', $shop_id)->where('status', 'in', '3, 4')->sum('order_price');
                    break;
                default:
                    $order_price = 0;
                    break;
            }
            $order_price = $order_price ? $order_price : 0;

            return ['data'=>['money' => $money, 'bank_cards' => $bank_cards, 'await_clearing_price' => $order_price],'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 生成充值订单
    static public function creation_recharge_order($param) {
        if(!isset($param['member_id']) || !isset($param['money']) || !isset($param['type']) || !isset($param['source'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }
        try {
            $order_sn = date("YmdHis") . rand(10000, 99999); //订单号
            $data = [
                'member_id' => $param['member_id'],
                'member_type' => $param['type'],
                'order_sn' => $order_sn,
                'order_price' => $param['money'],
                'add_time' => time(),
                'source' => $param['source'],
            ];
            $order_id = db('order_recharge')->insertGetId($data);

            $data['id'] = $order_id;
            return ['code' => 200, 'data' => $data, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }

    }
}
